<?php

/*/
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/ / system-wide constants in the global namespace
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/*/

define('BP_TIME_START', microtime(true));

define('BP_DIR_APP', 'app');
define('BP_DIR_ACTION', 'action');
define('BP_DIR_CACHE', 'cache');
define('BP_DIR_CLIENT', 'client');
define('BP_DIR_CONTROL', 'control');
define('BP_DIR_GENERAL', 'general');
define('BP_DIR_ELEMENT', 'element');
define('BP_DIR_LANG', 'lang');
define('BP_DIR_LIB', 'lib');
define('BP_DIR_LOG', 'log');
define('BP_DIR_MODEL', 'model');
define('BP_DIR_MODULE', 'module');
define('BP_DIR_OUTPUT', 'out');
define('BP_DIR_PUBLIC', 'pub');
define('BP_DIR_SCRIPT', 'script');
define('BP_DIR_SERVER', 'server');
define('BP_DIR_STYLE', 'style');
define('BP_DIR_SYSTEM', 'sys');
define('BP_DIR_UTIL', 'util');
define('BP_DIR_VIEW', 'view');
define('BP_DIR_VM', 'vm');

define('BP_EXT_CONFIG', '.xml');
define('BP_EXT_MANIFEST', '.manifest');
define('BP_EXT_JSON', '.json');
define('BP_EXT_PACK', '.pak');
define('BP_EXT_PHP', '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));
define('BP_EXT_SCRIPT', '.js');
define('BP_EXT_STYLE', '.css');
define('BP_EXT_TEMPLATE', '.tpl');

// when memory caching is turned on (default is off) this is how long in seconds a cache is valid
define('BP_CACHE_TIME', 86400);

// this is used in production mode to seed the error number before converting it to octal and
// showing it to the end user, set it to something unique to this installation
define('BP_ERR_SEED', 100);

define('BP_SYS_CLI', (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])));
define('BP_SYS_CHARSET', 'utf-8');
define('BP_SYS_NAME', 'BytePattern');
define('BP_SYS_VER', 0.1);

// always use these when including files to avoid path errors with CLI requests
define('BP_PATH_PUBLIC', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
define('BP_PATH_ROOT', dirname(BP_PATH_PUBLIC) . DIRECTORY_SEPARATOR);
define('BP_PATH_APP', BP_PATH_ROOT . BP_DIR_APP . DIRECTORY_SEPARATOR);
define('BP_PATH_LOG', BP_PATH_ROOT . BP_DIR_LOG . DIRECTORY_SEPARATOR);
define('BP_PATH_SYSTEM', BP_PATH_ROOT . BP_DIR_SYSTEM . DIRECTORY_SEPARATOR);
define('BP_PATH_UTIL', BP_PATH_ROOT . BP_DIR_UTIL . DIRECTORY_SEPARATOR);

define('BP_PATH_APP_CLIENT', BP_PATH_APP . BP_DIR_CLIENT . DIRECTORY_SEPARATOR);
define('BP_PATH_APP_SERVER', BP_PATH_APP . BP_DIR_SERVER . DIRECTORY_SEPARATOR);
define('BP_PATH_APP_VIEW', BP_PATH_APP . BP_DIR_VIEW . DIRECTORY_SEPARATOR);

define('BP_PATH_SYS_CACHE', BP_PATH_SYSTEM . BP_DIR_CACHE . DIRECTORY_SEPARATOR);
define('BP_PATH_SYS_CACHE_APP', BP_PATH_SYS_CACHE . BP_DIR_APP . DIRECTORY_SEPARATOR);
define('BP_PATH_SYS_CACHE_OUTPUT', BP_PATH_SYS_CACHE . BP_DIR_OUTPUT . DIRECTORY_SEPARATOR);

/*/
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/ / initial checks before the bootstrap
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/*/

// these should be the only non-formatted output strings ever sent to STDOUT
(version_compare(PHP_VERSION, '5.5.0') >= 0) or exit('PHP 5.5 or newer needs to be installed.');

// this stub file is one the few areas in the system we use inline literals to present info to the user
is_dir(BP_PATH_LOG) or exit('The log file directory is missing.');
is_dir(BP_PATH_UTIL) or exit('The utility directory is missing.');

// ensure the required directories are present, optional ones are loaded as needed, assume if we have the required
// subdirectories installed then we also have the root application and system directories installed as well
is_dir(BP_PATH_APP_CLIENT . BP_DIR_LANG . DIRECTORY_SEPARATOR) or exit('The application client localization directory is missing.');
is_dir(BP_PATH_APP_CLIENT . BP_DIR_SCRIPT . DIRECTORY_SEPARATOR) or exit('The application client script directory is missing.');
is_dir(BP_PATH_APP_CLIENT . BP_DIR_STYLE . DIRECTORY_SEPARATOR) or exit('The application client style directory is missing.');
is_dir(BP_PATH_APP_CLIENT . BP_DIR_VM . DIRECTORY_SEPARATOR) or exit('The application client view model directory is missing.');

is_dir(BP_PATH_APP_SERVER . BP_DIR_CONTROL) or exit('The application controller directory is missing.');
is_dir(BP_PATH_APP_SERVER . BP_DIR_GENERAL) or exit('The application general class directory is missing.');
is_dir(BP_PATH_APP_SERVER . BP_DIR_LANG) or exit('The application localization directory is missing.');
is_dir(BP_PATH_APP_SERVER . BP_DIR_MODEL) or exit('The application model directory is missing.');

is_dir(BP_PATH_APP_VIEW . BP_DIR_ACTION) or exit('The application view action directory is missing.');
is_dir(BP_PATH_APP_VIEW . BP_DIR_ELEMENT) or exit('The application view element directory is missing.');

// create the cache directory if it does not exist
is_dir(BP_PATH_SYSTEM . BP_DIR_CACHE) or mkdir(BP_PATH_SYSTEM . BP_DIR_CACHE);

is_dir(BP_PATH_SYS_CACHE_APP . BP_DIR_LANG . DIRECTORY_SEPARATOR) or exit('The system application localization cache directory is missing.');
is_dir(BP_PATH_SYS_CACHE_APP . BP_DIR_SCRIPT . DIRECTORY_SEPARATOR) or exit('The system application script cache directory is missing.');
is_dir(BP_PATH_SYS_CACHE_APP . BP_DIR_STYLE . DIRECTORY_SEPARATOR) or exit('The system application style cache directory is missing.');
is_dir(BP_PATH_SYS_CACHE_APP . BP_DIR_VM . DIRECTORY_SEPARATOR) or exit('The system application view model cache directory is missing.');
is_dir(BP_PATH_SYS_CACHE_OUTPUT) or exit('The system output cache directory is missing.');

is_dir(BP_PATH_SYSTEM . BP_DIR_LANG) or exit('The system localization directory is missing.');
is_dir(BP_PATH_SYSTEM . BP_DIR_MODULE) or exit('The system module directory is missing.');
is_dir(BP_PATH_SYSTEM . BP_DIR_SCRIPT) or exit('The system script directory is missing.');

// after the configuration and language files are processed, only localized strings should be displayed
is_file(BP_PATH_ROOT . 'config' . BP_EXT_CONFIG) or exit('Missing configuration file.');

// include the bare minimum we need to get the system going (base loads the language module)
require_once BP_PATH_SYSTEM . 'base' . BP_EXT_PHP;
require_once BP_PATH_SYSTEM . 'error' . BP_EXT_PHP;

/*/
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/ / for security, install error handling hooks before the bootstrap
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/*/

set_exception_handler(array('\\System\\Error', 'onException'));
set_error_handler(array('\\System\\Error', 'onError'));

/*/
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/ / convenience routines used with retrieving custom config values and localization strings
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/*/

function _c($key) { return \System\Base::getInstance()->getCustomValue($key); }
function _l($key, $system = false) { return \System\Base::getInstance()->getLangValue($key, $system); }

/*/
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/ / system entry point, let's run this sucker
/ / --------------------------------------------------------------------------------------------------------------------------------------------------
/*/

// fire this sucker off
\System\Base::getInstance()->initialize();

?>